using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS.DAO
{
	public class CustomerDAO
	{
		#region Fields

		private string connectionStringName;

		#endregion

		#region Constructors

		public CustomerDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Customer table.
		/// </summary>
		public void Insert(CustomerDTO customer)
		{
			ValidationUtility.ValidateArgument("customer", customer);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", customer.Id),
				new SqlParameter("@Name", customer.Name),
				new SqlParameter("@Address", customer.Address),
				new SqlParameter("@Active", customer.Active),
				new SqlParameter("@Email", customer.Email),
				new SqlParameter("@Website", customer.Website),
				new SqlParameter("@Phone1", customer.Phone1),
				new SqlParameter("@Phone2", customer.Phone2),
                new SqlParameter("@Picture", SqlDbType.VarBinary, -1)				
			};
            parameters[8].Value = customer.Picture;
			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_CustomerInsert", parameters);
		}

		/// <summary>
		/// Updates a record in the Customer table.
		/// </summary>
		public void Update(CustomerDTO customer)
		{
			ValidationUtility.ValidateArgument("customer", customer);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", customer.Id),
				new SqlParameter("@Name", customer.Name),
				new SqlParameter("@Address", customer.Address),
				new SqlParameter("@Active", customer.Active),
				new SqlParameter("@Email", customer.Email),
				new SqlParameter("@Website", customer.Website),
				new SqlParameter("@Phone1", customer.Phone1),
				new SqlParameter("@Phone2", customer.Phone2),
				new SqlParameter("@Picture", SqlDbType.VarBinary, -1)
			};
            parameters[8].Value = customer.Picture;
			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_CustomerUpdate", parameters);
		}

		/// <summary>
		/// Deletes a record from the sp_Customer table by its primary key.
		/// </summary>
		public void Delete(string id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_CustomerDelete", parameters);
		}

		/// <summary>
		/// Selects a single record from the Customer table.
		/// </summary>
		public CustomerDTO Select(string id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_CustomerSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects a single record from the Customer table.
		/// </summary>
		public string SelectJson(string id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_CustomerSelect", parameters);
		}

		/// <summary>
		/// Selects all records from the Customer table.
		/// </summary>
		public List<CustomerDTO> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_CustomerSelectAll"))
			{
				List<CustomerDTO> customerDTOList = new List<CustomerDTO>();
				while (dataReader.Read())
				{
					CustomerDTO customerDTO = MapDataReader(dataReader);
					customerDTOList.Add(customerDTO);
				}

				return customerDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the Customer table.
		/// </summary>
		public string SelectAllJson()
		{
			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_CustomerSelectAll");
		}

		/// <summary>
		/// Creates a new instance of the CustomerDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private CustomerDTO MapDataReader(SqlDataReader dataReader)
		{
			CustomerDTO customerDTO = new CustomerDTO();
			customerDTO.Id = dataReader.GetString("Id", String.Empty);
			customerDTO.Name = dataReader.GetString("Name", null);
			customerDTO.Address = dataReader.GetString("Address", null);
			customerDTO.Active = dataReader.GetBoolean("Active", false);
			customerDTO.Email = dataReader.GetString("Email", String.Empty);
			customerDTO.Website = dataReader.GetString("Website", String.Empty);
			customerDTO.Phone1 = dataReader.GetString("Phone1", String.Empty);
			customerDTO.Phone2 = dataReader.GetString("Phone2", String.Empty);
			customerDTO.Picture = dataReader.GetBytes("Picture", new byte[0]);

			return customerDTO;
		}

		#endregion
	}
}
